package com.sugar.subscribe.course.module.business.controller.admin.withdrawrecord;

import com.sugar.subscribe.course.module.pay.api.notify.dto.PayTransferNotifyReqDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.CommonResult;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;
import static com.sugar.subscribe.course.framework.common.pojo.CommonResult.success;

import com.sugar.subscribe.course.framework.excel.core.util.ExcelUtils;

import com.sugar.subscribe.course.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.subscribe.course.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.subscribe.course.module.business.controller.admin.withdrawrecord.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.withdrawrecord.WithdrawRecordDO;
import com.sugar.subscribe.course.module.business.service.withdrawrecord.WithdrawRecordService;

@Tag(name = "管理后台 - 提现记录")
@RestController
@RequestMapping("/business/withdraw-record")
@Validated
public class WithdrawRecordController {

    @Resource
    private WithdrawRecordService withdrawRecordService;

    @PutMapping("/update")
    @Operation(summary = "审核提现记录")
    @PreAuthorize("@ss.hasPermission('business:withdraw-record:update')")
    public CommonResult<Boolean> updateWithdrawRecord(@Valid @RequestBody WithdrawRecordSaveReqVO updateReqVO) {
        withdrawRecordService.updateWithdrawRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除提现记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:withdraw-record:delete')")
    public CommonResult<Boolean> deleteWithdrawRecord(@RequestParam("id") Long id) {
        withdrawRecordService.deleteWithdrawRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除提现记录")
                @PreAuthorize("@ss.hasPermission('business:withdraw-record:delete')")
    public CommonResult<Boolean> deleteWithdrawRecordList(@RequestParam("ids") List<Long> ids) {
        withdrawRecordService.deleteWithdrawRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得提现记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:withdraw-record:query')")
    public CommonResult<WithdrawRecordRespVO> getWithdrawRecord(@RequestParam("id") Long id) {
        WithdrawRecordDO withdrawRecord = withdrawRecordService.getWithdrawRecord(id);
        return success(BeanUtils.toBean(withdrawRecord, WithdrawRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得提现记录分页")
    @PreAuthorize("@ss.hasPermission('business:withdraw-record:query')")
    public CommonResult<PageResult<WithdrawRecordRespVO>> getWithdrawRecordPage(@Valid WithdrawRecordPageReqVO pageReqVO) {
        return success(withdrawRecordService.getWithdrawRecordPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出提现记录 Excel")
    @PreAuthorize("@ss.hasPermission('business:withdraw-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWithdrawRecordExcel(@Valid WithdrawRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 导出 Excel
        ExcelUtils.write(response, "提现记录.xls", "数据", WithdrawRecordRespVO.class,
                withdrawRecordService.getWithdrawRecordPage(pageReqVO).getList());
    }

    @PostMapping("/update-transferred")
    @Operation(summary = "更新转账状态") // 由 pay-module 支付服务，进行回调，可见 PayNotifyJob
    @PermitAll // 无需登录，安全由 BusinessOrderService 内部校验实现
    public CommonResult<Boolean> updateTransferOrder(@RequestBody PayTransferNotifyReqDTO notifyReqDTO) {
        withdrawRecordService.updateTransferOrder(Long.parseLong(notifyReqDTO.getMerchantTransferId()),
                notifyReqDTO.getPayTransferId());
        return success(true);
    }
}